{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 日夜图像分类器\n",
    "---\n",
    "\n",
    "日夜图像数据集由200个RGB彩色图像组成，分为两类：白天图像和夜晚图像。每个例子都有相同的数字：100个日图像和100个夜图像。\n",
    "\n",
    "我们希望建立一个分类器，可以把这些图像准确地标记为白天或黑夜。要完成这个任务，我们需要找出这两种图像之间的显著性特征！\n",
    "\n",
    "*注：所有图像都来自 [AMOS 数据集](http://cs.uky.edu/~jacobs/datasets/amos/) （众多户外场景档案）。*\n",
    "\n",
    "\n",
    "### 导入资源\n",
    "\n",
    "在开始使用项目代码之前，请导入你需要的库和资源。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2 # computer vision library\n",
    "import helpers\n",
    "\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.image as mpimg\n",
    "\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 训练并测试数据\n",
    "200张日/夜的图像被分成训练和测试数据集。\n",
    "\n",
    "* 这些图像中的60％是训练图像，供你在创建分类器时使用。\n",
    "* 另外40％是测试图像，将用于测试分类器的准确度。\n",
    "\n",
    "首先，我们设置一些变量来跟踪图像的存储位置：\n",
    "\n",
    "image_dir_training: the directory where our training image data is stored\n",
    "image_dir_test: the directory where our test image data is stored"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Image data directories\n",
    "image_dir_training = \"day_night_images/training/\"\n",
    "image_dir_test = \"day_night_images/test/\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 加载数据集\n",
    "\n",
    "前几行代码将加载训练日/夜图像，并将它们全部存储在变量`IMAGE_LIST`中。 该列表包含图像及其相关标签（“日”或“夜”）。\n",
    "\n",
    "例如， `IMAGE_LIST` 中的第一个图像标签对可以通过索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Using the load_dataset function in helpers.py\n",
    "# Load training data\n",
    "IMAGE_LIST = helpers.load_dataset(image_dir_training)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "# 1. 可视化输入图像"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Print out 1. The shape of the image and 2. The image's label\n",
    "\n",
    "# Select an image and its label by list index\n",
    "image_index = 0\n",
    "selected_image = IMAGE_LIST[image_index][0]\n",
    "selected_label = IMAGE_LIST[image_index][1]\n",
    "\n",
    "# Display image and data about it\n",
    "plt.imshow(selected_image)\n",
    "print(\"Shape: \"+str(selected_image.shape))\n",
    "print(\"Label: \" + str(selected_label))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. 预处理数据\n",
    "\n",
    "在每张图像中进行加载之后，你需要可视化输入和输出！\n",
    "\n",
    "\n",
    "---\n",
    "### 输入\n",
    "\n",
    "\n",
    "请使你的图像尺寸保持一致，以便传输至相同的分类步骤！每一张输入图像应拥有相同的格式、尺寸，等等。\n",
    "\n",
    "#### TODO: 标准化输入图像\n",
    "\n",
    "* 重新将每张图像调整为要求尺寸: 600x1100px (hxw)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# This function should take in an RGB image and return a new, standardized version\n",
    "def standardize_input(image):\n",
    "    \n",
    "    ## TODO: Resize image so that all \"standard\" images are the same size 600x1100 (hxw) \n",
    "    standard_im = []\n",
    "    \n",
    "    return standard_im\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### TODO: 标准化输出\n",
    "\n",
    "对于每一张加载的图像，你还需要确定预期输出。针对这一点，请使用二进制数值，0/1=夜/日。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Examples: \n",
    "# encode(\"day\") should return: 1\n",
    "# encode(\"night\") should return: 0\n",
    "\n",
    "def encode(label):\n",
    "        \n",
    "    numerical_val = 0\n",
    "    ## TODO: complete the code to produce a numerical label\n",
    "    \n",
    "    return numerical_val\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 构建输入图像和输出标签的`STANDARDIZED_LIST` 函数 \n",
    "\n",
    "该函数将输入一个图像标签对列表，并输出一个包含调整过的图像和数字标签的**标准**列表。\n",
    "\n",
    "此处使用你在上方定义的函数来标准化输入和输出，因此，你必须完成那些函数来使这里的标准化顺利进行。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def standardize(image_list):\n",
    "    \n",
    "    # Empty image data array\n",
    "    standard_list = []\n",
    "\n",
    "    # Iterate through all the image-label pairs\n",
    "    for item in image_list:\n",
    "        image = item[0]\n",
    "        label = item[1]\n",
    "\n",
    "        # Standardize the image\n",
    "        standardized_im = standardize_input(image)\n",
    "\n",
    "        # Create a numerical label\n",
    "        binary_label = encode(label)    \n",
    "\n",
    "        # Append the image, and it's one hot encoded label to the full, processed list of image data \n",
    "        standard_list.append((standardized_im, binary_label))\n",
    "        \n",
    "    return standard_list\n",
    "\n",
    "# Standardize all training images\n",
    "STANDARDIZED_LIST = standardize(IMAGE_LIST)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 将标准化数据可视化\n",
    "\n",
    "显示一个来自STANDARDIZED_LIST的标准化图片。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Display a standardized image and its label\n",
    "\n",
    "# Select an image by index\n",
    "image_num = 0\n",
    "selected_image = STANDARDIZED_LIST[image_num][0]\n",
    "selected_label = STANDARDIZED_LIST[image_num][1]\n",
    "\n",
    "# Display image and data about it\n",
    "## TODO: Make sure the images have numerical labels and are of the same size\n",
    "plt.imshow(selected_image)\n",
    "print(\"Shape: \"+str(selected_image.shape))\n",
    "print(\"Label [1 = day, 0 = night]: \" + str(selected_label))\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
